| name | value | location | spawn_low | spawn_high |
|---|---|---|---|---|
| giant trevally | 4500 | Pier | 1 | 1 |
| blowfish | 5000 | Sea | 5 | 5 |
| sturgeon | 10000 | River | 1 | 4 |
| angelfish | 3000 | River | 2 | 5 |
| olive flounder | 800 | Sea | 4 | 6 |
DSST 289: Introduction to Data Science
2024-09-09
mutate()if_else()case_when()What’s the best strategy to get rich fishing?
Fishing in Animal Crossing
| name | value | location | spawn_low | spawn_high |
|---|---|---|---|---|
| giant trevally | 4500 | Pier | 1 | 1 |
| blowfish | 5000 | Sea | 5 | 5 |
| sturgeon | 10000 | River | 1 | 4 |
| angelfish | 3000 | River | 2 | 5 |
| olive flounder | 800 | Sea | 4 | 6 |
mutateing spawn rates# A tibble: 5 × 4
name spawn_low spawn_high spawn_rate
<chr> <dbl> <dbl> <dbl>
1 salmon 20 20 20
2 pond smelt 18 20 19
3 horse mackerel 14 21 17.5
4 bitterling 12 17 14.5
5 sea bass 11 18 14.5
mutate with if_else# A tibble: 5 × 3
name spawn_rate spawn_freq
<chr> <dbl> <chr>
1 bitterling 14.5 common
2 sea butterfly 10.5 common
3 ranchu goldfish 1.5 rare
4 puffer fish 7.5 rare
5 sea horse 6 rare
case_when for multiple categoriesfish |>
# get rid of common or very common fish:
filter(!spawn_freq %in% c("common", "very common")) |>
# only keep fish above median value:
filter(value > median(value)) |>
ggplot(aes(x = spawn_rate, y = value, color = location)) +
geom_point() +
geom_text_repel(aes(label = name)) +
geom_segment(aes(x = 0,
y = 0,
xend = max(spawn_rate),
yend = max(value)),
linetype = "dashed",
color = "gray",
alpha = 0.1)fish |>
group_by(location) |>
mutate(spawn_prob = spawn_rate / sum(spawn_rate)) |>
ungroup() |>
select(name, spawn_rate, location, spawn_prob) |>
slice_sample(n = 5)# A tibble: 5 × 4
name spawn_rate location spawn_prob
<chr> <dbl> <chr> <dbl>
1 arapaima 1 River 0.00539
2 sweetfish 7.5 River 0.0404
3 butterfly fish 4.5 Sea 0.0312
4 king salmon 5 River 0.0270
5 pale chub 7.5 River 0.0404
https://fredner.org